package com.hualan.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.mapper.QuestionMapper;
import com.hualan.sfzy.model.entity.Answer;
import com.hualan.sfzy.model.entity.Question;
import com.hualan.sfzy.model.vo.AnswerCount;
import com.hualan.sfzy.service.BaseService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QuestionServiceImpl implements BaseService<Question> {

    @Autowired
    private QuestionMapper questionMapper;

    /*新增*/
    @Override
    public int add(Question question) {
        return questionMapper.insert(question);
    }

    /*修改*/
    @Override
    public int edit(Question question) {
        return questionMapper.update(question);
    }

    /*删除*/
    @Override
    public int remove(Integer id) {
        return questionMapper.delete(id);
    }

    /*批量删除*/
    @Override
    public int removeBatch(Integer[] ids) {
        return questionMapper.deleteBatch(ids);
    }

    /*查询全部*/
    @Override
    public List<Question> find() {
        return questionMapper.select(null);
    }

    /*分页查询*/
    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, Question question) {
        PageHelper.startPage(pageNum, pageSize);
        List<Question> list = questionMapper.select(question);
        PageInfo<Question> pageInfo = new PageInfo<>(list); // 分页信息对象
        Map<String, Object> map = new HashMap<>();
        // map存储当前页数据、总页数、总记录数
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

    /*根据id查询单个*/
    @Override
    public Question findById(Integer id) {
        return questionMapper.selectById(id);
    }
    /*根据id查询单个(多表)*/
    public Question selectByIdDuo(Integer id) {
        return questionMapper.selectByIdDuo(id);
    }


    /*查询全部(多表) 带条件*/
    public Map<String, Object> selectDuo(Integer pageNum, Integer pageSize, Question entity) {
        PageHelper.startPage(pageNum, pageSize);
        List<Question> list = questionMapper.selectDuo(entity);
        PageInfo<Question> pageInfo = new PageInfo<>(list); // 分页信息对象
        Map<String, Object> map = new HashMap<>();
        // map存储当前页数据、总页数、总记录数
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

    /*根据问题id查询回答数量*/
    public List<AnswerCount> selectByQuestionId() {
        return questionMapper.selectCountByQuestionId();
    }
}
